function msg = decodeimage(coef, k, rands)
%DECODEIMAGE
%  MSG = DECODEIMAGE(COEF, K, RANDS)
    
    global DEBUG
    
%% skip DC
    [M0, N0] = size(coef);
    sym_o = im2vec(coef,[8 8])';
    ac_o = sym_o(:,2:end);
    
%% embed order
    [M, N] = size(ac_o);
    %order = prand1(M*N, rands);
    order = prand3(M*N, rands);
    ac_o = ac_o(order(1,:));
    
%% create bin
    bin = zeros(1,M*N+k);
    n = 2^k - 1;
    
%% decoding
    msg_count = 1;
    ind = find(ac_o ~= 0); % nonzero DCT
    for i = 1:n:length(ind)
        if (i + n-1 <= length(ind))
            current_coef = ac_o(ind(i:i+n-1));
            current_msg = get_value(current_coef,k,n);
            bin(msg_count:msg_count+k-1) = current_msg;
            msg_count = msg_count + k;
            if DEBUG
                for j = i:i+n-1
                    fprintf(1,'%d ',ind(j));
                end
                fprintf(1,'\t');
                for j = 1:n
                    fprintf(1,'%d ',current_coef(j));
                end
                fprintf(1,'\n');
                %pause(0.5);
            end
        else
            % forget the last several coef ~
            break;
        end
    end
   
    msg_count = msg_count - 1;
    bin = mod(bin(1,1:msg_count) + round(rands.rand(1,msg_count)), 2);
    msg = binaryToArray(bin, msg_count);
end
